package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Pagol extends Activity implements OnClickListener {

	Button allProjects;
	Button viewProfile;
	Button ppf;
	Button psf;
	Button midpoint;
	Button addDiary;
	Button viewDiary;
	Button viewMeeting;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pagol);

		allProjects = (Button) findViewById(R.id.btnsearchprogects);
		viewProfile = (Button) findViewById(R.id.btnpagolprofile);
		ppf = (Button) findViewById(R.id.btnppf);
		psf = (Button) findViewById(R.id.btnpsf);
		midpoint = (Button) findViewById(R.id.btnmidpoint);
		addDiary = (Button) findViewById(R.id.btnadddiary);
		viewDiary = (Button) findViewById(R.id.btnviewdiary);
		viewMeeting = (Button) findViewById(R.id.btnviewmeeting);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.pagol, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == allProjects) {
			Intent i = new Intent(this,AllFypProjects.class);
			startActivity(i);
		}
		if (v == viewProfile) {
			//http://webspace1.apiit.lk/pagol15/www/student_pp.asp
			//Get
			
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/pagol15/www/student_pp.asp");
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("studentID","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[2]/td[3]---");
			xpathMap.put("intakeID","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[3]/td[3]---");
			xpathMap.put("studentName","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[4]/td[3]---");
			xpathMap.put("email","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[5]/td[3]---");
			xpathMap.put("contactNo","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[6]/td[3]---");
			xpathMap.put("proTitle","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[7]/td[3]---");
			xpathMap.put("supervisor","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[8]/td[3]/a[1]---");
			xpathMap.put("advisor","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[9]/td[3]/a[1]---");
			xpathMap.put("ppfDate","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[10]/td[3]---");
			xpathMap.put("psfDate","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[11]/td[3]---");
			xpathMap.put("midDate","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[12]/td[3]---");
			xpathMap.put("subDate","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[13]/td[3]---");
			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			
			BPEL_Response respones = null;

			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String studendID = respones.getResultsMap().get("studentID").trim();
			String intakeID = respones.getResultsMap().get("intakeID").trim();
			String studentName = respones.getResultsMap().get("studentName").trim();
			String email = respones.getResultsMap().get("email").trim();
			String contactno = respones.getResultsMap().get("contactNo").trim();
			String projTitle = respones.getResultsMap().get("proTitle").trim();
			String supervisor = respones.getResultsMap().get("supervisor").trim();
			String advisor = respones.getResultsMap().get("advisor").trim();
			String ppfDate = respones.getResultsMap().get("ppfDate").trim();
			String psfDate = respones.getResultsMap().get("psfDate").trim();
			String midDate = respones.getResultsMap().get("midDate").trim();
			String subDate = respones.getResultsMap().get("subDate").trim();
			
			Toast.makeText(getApplicationContext(), studendID,
					Toast.LENGTH_LONG).show();
			
			Intent i = new Intent(this, Viewpagolprofile.class);			
			i.putExtra("studID", studendID);
			i.putExtra("intakeID", intakeID);
			i.putExtra("studName", studentName);
			i.putExtra("email", email);
			i.putExtra("contactNo", contactno);
			i.putExtra("projTitle", projTitle);
			i.putExtra("supervisor", supervisor);
			i.putExtra("advisor", advisor);
			i.putExtra("ppfDate", ppfDate);
			i.putExtra("psfDate", psfDate);
			i.putExtra("midDate", midDate);
			i.putExtra("subDate", subDate);
			startActivity(i);
		}
		if (v == ppf) {
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/pagol15/www/student_ppf.asp");
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("proTitle","/html[1]/body[1]/center[1]/table[2]/tr[1]/td[1]/table[1]/tr[3]/td[2]---");
			xpathMap.put("systemType","/html[1]/body[1]/center[1]/table[2]/tr[1]/td[1]/table[1]/tr[5]/td[2]---");
			xpathMap.put("developedFor","/html[1]/body[1]/center[1]/table[2]/tr[1]/td[1]/table[1]/tr[7]/td[2]---");
			xpathMap.put("requiredFor","/html[1]/body[1]/center[1]/table[2]/tr[1]/td[1]/table[1]/tr[9]/td[2]---");
			xpathMap.put("challenges","/html[1]/body[1]/center[1]/table[2]/tr[1]/td[1]/table[1]/tr[11]/td[2]---");
			xpathMap.put("newIdeas","/html[1]/body[1]/center[1]/table[2]/tr[1]/td[1]/table[1]/tr[13]/td[2]---");

			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			
			BPEL_Response respones = null;

			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String proTitle = respones.getResultsMap().get("proTitle").trim();
			String systemType = respones.getResultsMap().get("systemType").trim();
			String developedFor = respones.getResultsMap().get("developedFor").trim();
			String requiredFor = respones.getResultsMap().get("requiredFor").trim();
			String challenges = respones.getResultsMap().get("challenges").trim();
			String newIdeas = respones.getResultsMap().get("newIdeas").trim();

			
			Toast.makeText(getApplicationContext(), newIdeas,
					Toast.LENGTH_LONG).show();
			
			Intent i = new Intent(this, ViewPPF.class);			
			i.putExtra("proTitle", proTitle);
			i.putExtra("systemType", systemType);
			i.putExtra("developedFor", developedFor);
			i.putExtra("requiredFor", requiredFor);
			i.putExtra("challenges", challenges);
			i.putExtra("newIdeas", newIdeas);

			startActivity(i);
		}
		if (v == psf) {
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/pagol15/www/student_psf.asp");
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("background","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[3]/tr[3]/td[2]---");
			xpathMap.put("objectives","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[3]/tr[6]/td[2]---");
			xpathMap.put("resources","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[3]/tr[9]/td[2]---");
			xpathMap.put("research","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[3]/tr[12]/td[2]---");
			xpathMap.put("plan","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[3]/tr[15]/td[2]---");
			xpathMap.put("testPlan","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[3]/tr[18]/td[2]---");
			
			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			
			BPEL_Response respones = null;

			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String backgroundValue = respones.getResultsMap().get("background").trim();
			String objectivesValue = respones.getResultsMap().get("objectives").trim();
			String resourcesValue = respones.getResultsMap().get("resources").trim();
			String researchValue = respones.getResultsMap().get("research").trim();
			String planVallue = respones.getResultsMap().get("plan").trim();
			String testPlanValue = respones.getResultsMap().get("testPlan").trim();
//			
			Toast.makeText(getApplicationContext(), backgroundValue + resourcesValue + researchValue + planVallue + testPlanValue,
					Toast.LENGTH_LONG).show();
			
			Intent i = new Intent(this, ViewPSF.class);			
			i.putExtra("background", backgroundValue);
			i.putExtra("objectives", objectivesValue);
			i.putExtra("resources", resourcesValue);
			i.putExtra("research", researchValue);
			i.putExtra("plan", planVallue);
			i.putExtra("testPlan", testPlanValue);
			startActivity(i);
		}
		if (v == midpoint) {
			
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/pagol15/www/student_midpoint.asp");
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("studName","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[2]/tr[1]/td[4]---");
			xpathMap.put("studID","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[2]/tr[2]/td[4]---");
			xpathMap.put("title","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[2]/tr[3]/td[4]---");
			xpathMap.put("supervisor","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[2]/tr[4]/td[4]---");
			xpathMap.put("advisor","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[2]/tr[5]/td[4]---");
			
			xpathMap.put("objectiveComment","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[3]/tr[3]/td[2]---");
			xpathMap.put("researchComment","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[3]/tr[6]/td[2]---");
			xpathMap.put("methodologyComment","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[3]/tr[9]/td[2]---");
			xpathMap.put("languageComment","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[3]/tr[12]/td[2]---");
			xpathMap.put("additionalComments","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[3]/tr[15]/td[2]---");

			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			
			BPEL_Response respones = null;

			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String studName = respones.getResultsMap().get("studName").trim();
			String studID = respones.getResultsMap().get("studID").trim();
			String title = respones.getResultsMap().get("title").trim();
			String supervisor = respones.getResultsMap().get("supervisor").trim();
			String advisor = respones.getResultsMap().get("advisor").trim();
			
			String objectiveComment = respones.getResultsMap().get("objectiveComment").trim();
			String objectivesValue = respones.getResultsMap().get("researchComment").trim();
			String resourcesValue = respones.getResultsMap().get("methodologyComment").trim();
			String researchValue = respones.getResultsMap().get("languageComment").trim();
			String planVallue = respones.getResultsMap().get("additionalComments").trim();

			Toast.makeText(getApplicationContext(), objectiveComment,
					Toast.LENGTH_LONG).show();
			
			Intent i = new Intent(this, ViewMidPoint.class);	
			i.putExtra("studName", studName);
			i.putExtra("studID", studID);
			i.putExtra("title", title);
			i.putExtra("supervisor", supervisor);
			i.putExtra("advisor", advisor);
			
			i.putExtra("objectiveComment", objectiveComment);
			i.putExtra("researchComment", objectivesValue);
			i.putExtra("methodologyComment", resourcesValue);
			i.putExtra("languageComment", researchValue);
			i.putExtra("additionalComments", planVallue);
			startActivity(i);
		}
		if (v == addDiary) {
			Intent i = new Intent(this, AddDiaryEntry.class);
			startActivity(i);
		}
		if (v == viewDiary) {
			Intent i = new Intent(this, ViewDiary.class);
			startActivity(i);
		}
		if (v == viewMeeting) {
			Intent i = new Intent(this, ViewMeetings.class);
			startActivity(i);
		}

	}

}
